package com.ruoyi.project.mobile.jiaqin;

import com.ruoyi.project.jiaqin.downquotation.service.IDownQuotationService;
import com.ruoyi.project.jiaqin.quotation.domain.Quotation;
import com.ruoyi.project.jiaqin.quotation.service.IQuotationService;
import com.ruoyi.project.system.goodsteam.service.IGoodsTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 定时改变过期拼团订单状态
 *
 * @autor LCL
 * @date - 2021/3/6
 */
@Component
public class TimeTask {

    @Autowired
    private IGoodsTeamService goodsTeamService;
    @Autowired
    private IQuotationService iQuotationService;
    @Autowired
    private IDownQuotationService iDownQuotationService;
    @Autowired
    private IQuotationService quotationService;

    //编写具体的定时任务组件（@Component注解），并且在需要定时调度的方法上添加@Scheduled触发器
    //每隔5秒执行一次：*/5 * * * * ?
    //每隔1分钟执行一次：0 */1 * * * ?
    //每天23点执行一次：0 0 23 * * ?
    //从0时开始，每小时执行一次：0 0 0/1 * * ?
    //从0时开始，每2小时执行一次：0 0 0/2 * * ?
    //每天凌晨1点执行一次：0 0 1 * * ?
    //每月1号凌晨1点执行一次：0 0 1 1 * ?
    //每月最后一天23点执行一次：0 0 23 L * ?
    //每周星期天凌晨1点实行一次：0 0 1 ? * L
    //在26分、29分、33分执行一次：0 26,29,33 * * * ?
    //每天的0点、13点、18点、21点都执行一次：0 0 0,13,18,21 * * ?
    @Scheduled(cron = "0 0 1 * * ?")
    public void ssgx() throws ParseException {
        System.out.println("定时启动");
//        goodsTeamService.updateOrderTeamOrderStatus();//更新状态的业务逻辑

        Integer twoClass = iQuotationService.twoClassAvgStatistic();//二级分类均价计算定时任务 每日凌晨一点计算均价插入数据库
        System.out.println("二级分类均价计算启动=" + twoClass);

        Integer thrClass = iQuotationService.thrClassAvgStatistic();//三级分类均价计算定时任务 每日凌晨一点计算均价插入数据库
        System.out.println("三级分类均价计算启动=" + thrClass);

        Integer downAvg = iDownQuotationService.downAvgStatistic();//羽绒均价计算定时任务 每日凌晨一点计算均价插入数据库
        System.out.println("羽绒均价计算启动=" + downAvg);

        //假删除行情
        Quotation quotation = new Quotation();
        quotation.setDelFlag(0);
        List<Quotation> quotationList = quotationService.selectQuotationList(quotation);
        for (Quotation Q : quotationList) {
            Date nowDate = new Date();//当前时间
            Calendar cal = Calendar.getInstance();
            cal.setTime(nowDate);
            cal.add(Calendar.DATE, -30);//30天前
            //删除30天前记录
            if (cal.getTime().compareTo(Q.getReleaseDate()) > 0) {
                quotationService.deleteQuotationById(Q.getId());
            }
        }
    }

}
